package com.hc.oauthserver.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
import org.springframework.web.bind.annotation.ControllerAdvice;
import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class HandlerControllerException implements WebResponseExceptionTranslator {

    @Override
    public ResponseEntity translate(Exception e) throws Exception {
        Map<String, Object> map = new HashMap<>();
        if (e instanceof InvalidGrantException) {
            map.put("code", "400");
            map.put("message", "用户名密码错误");
        }
        if (e instanceof OAuth2Exception) {
            map.put("code", "500");
            map.put("message", "认证不同");
        }
        map.put("data",e.getMessage());
        map.put("success", false);
        ResponseEntity.status(HttpStatus.BANDWIDTH_LIMIT_EXCEEDED);
        return ResponseEntity.ok(map);
        //throw e;
    }
}
